package vn.edu.hut.soict.translate.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import vn.edu.hut.soict.translate.utils.LinkGrammarTester;
import java.awt.Font;

public class LinkGrammarTesterUI extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JButton btnParse;
	private LinkGrammarTester parser = new LinkGrammarTester();
	private JTextPane txtInput;
	private JTextPane txtOutput;
	private JButton btnReloadDictionary;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LinkGrammarTesterUI frame = new LinkGrammarTesterUI();
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LinkGrammarTesterUI() {
		setTitle("Link Grammar Parser");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 674, 460);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {
				0, 0, 0
		};
		gbl_contentPane.rowHeights = new int[] {
				114, 0, 0, 0
		};
		gbl_contentPane.columnWeights = new double[] {
				1.0, 0.0, Double.MIN_VALUE
		};
		gbl_contentPane.rowWeights = new double[] {
				0.0, 0.0, 1.0, Double.MIN_VALUE
		};
		contentPane.setLayout(gbl_contentPane);

		JScrollPane scrInput = new JScrollPane();
		scrInput.setBorder(new TitledBorder(null, "Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_scrInput = new GridBagConstraints();
		gbc_scrInput.gridwidth = 2;
		gbc_scrInput.fill = GridBagConstraints.BOTH;
		gbc_scrInput.insets = new Insets(0, 0, 5, 0);
		gbc_scrInput.gridx = 0;
		gbc_scrInput.gridy = 0;
		contentPane.add(scrInput, gbc_scrInput);

		txtInput = new JTextPane();
		scrInput.setViewportView(txtInput);

		btnParse = new JButton("Parse");
		btnParse.addActionListener(this);
		GridBagConstraints gbc_btnParse = new GridBagConstraints();
		gbc_btnParse.anchor = GridBagConstraints.EAST;
		gbc_btnParse.insets = new Insets(0, 0, 5, 5);
		gbc_btnParse.gridx = 0;
		gbc_btnParse.gridy = 1;
		contentPane.add(btnParse, gbc_btnParse);

		btnReloadDictionary = new JButton("Reload Dictionary");
		btnReloadDictionary.addActionListener(this);
		GridBagConstraints gbc_btnReloadDictionary = new GridBagConstraints();
		gbc_btnReloadDictionary.insets = new Insets(0, 0, 5, 0);
		gbc_btnReloadDictionary.gridx = 1;
		gbc_btnReloadDictionary.gridy = 1;
		contentPane.add(btnReloadDictionary, gbc_btnReloadDictionary);

		JScrollPane scrOutput = new JScrollPane();
		scrOutput.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Output", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		GridBagConstraints gbc_scrOutput = new GridBagConstraints();
		gbc_scrOutput.gridwidth = 2;
		gbc_scrOutput.fill = GridBagConstraints.BOTH;
		gbc_scrOutput.gridx = 0;
		gbc_scrOutput.gridy = 2;
		contentPane.add(scrOutput, gbc_scrOutput);

		txtOutput = new JTextPane();
		txtOutput.setFont(new Font("Courier New", Font.PLAIN, 12));
		scrOutput.setViewportView(txtOutput);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnReloadDictionary) {
			do_btnReloadDictionary_actionPerformed(arg0);
		}
		if (arg0.getSource() == btnParse) {
			do_btnParse_actionPerformed(arg0);
		}
	}

	protected void do_btnParse_actionPerformed(ActionEvent arg0) {
		String input = txtInput.getText();
		String output = parser.parse(input);
		if (output != null) {
			txtOutput.setText(output);
		}
		else {
			txtOutput.setText("No result");
		}
	}

	protected void do_btnReloadDictionary_actionPerformed(ActionEvent arg0) {
		parser.reloadDictionary();
	}
}
